package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Sport;
import com.itheima.service.SportService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sport")
public class SportController {

    @Reference
    private SportService sportService;

    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {

        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<Sport> data = new ArrayList<>();
            for (String[] strings : list) {
                //运动助记码
                String code = strings[0];
                //运动名称
                String name = strings[1];
                //运动次数
                String size = strings[2];
                //运动时间
                String runtime = strings[3];
                Sport sport = new Sport(code, name, size, runtime);
                data.add(sport);
            }
            sportService.addList(data);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }


    //新增运动库
    @RequestMapping("/add")
    public Result add(@RequestBody Sport sport) {
        try {
            sportService.add(sport);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SPORT_SUCCESS);
    }

    //查询运动库
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return sportService.pageQuery(queryPageBean);
    }

    //删除运动库
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            sportService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SPORT_SUCCESS);
    }

    //编辑运动库
    @RequestMapping("/edit")
    public Result edit(@RequestBody Sport sport) {
        try {
            sportService.edit(sport);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_SPORT_SUCCESS);
    }

    //查询一个运动库
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Sport sport = sportService.findById(id);
            return new Result(true, MessageConstant.QUERY_SPORT_SUCCESS,sport);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_SPORT_FAIL);
        }
    }

    //查所有
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Sport> list = sportService.findAll();
            return new Result(true, MessageConstant.QUERY_SPORT_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_SPORT_FAIL);
        }
    }
}
